<?php
namespace Baseapp\Models;

use Phalcon\Db\Column;

class Member extends \Phalcon\Mvc\Model
{

    public $id;

    public $group_id;

    public $username;

    public $password;

    public $nickname;

    public $realname;

    public $email;

    public $is_del;

    public $add_time;

    public static $lockStatusOptions = array(
        0 => '正常',
        1 =>'锁定'
    );

    public static $genderOptions = array(
        0 => '保密',
        1 => '男',
        2 =>'女'
    );

    public static $maritalStatusOptions = array(
        0 => '保密',
        1 => '未婚',
        2 =>'已婚'
    );




    public function initialize()
    {
        $this->belongsTo("type_id", "MemberType", "id");
        $this->belongsTo("rank_id", "MemberRank", "id");
    }

    public static function getSearchResult($filters = '')
    {

        $conditions = '';
        $parameters = array();

        if (!empty($filters['realname'])) {
            $conditions .= " realname = :realname: AND";
            $parameters['realname'] = $filters['realname'];
        }

        if (!empty($filters['username'])) {
            $conditions .= " username = :username: AND ";
            $parameters['username'] = $filters['username'];
        }

         if (!empty($filters['email'])) {
            $conditions .= " email = :email: AND";
            $parameters['email'] = $filters['email'];
        }

        if ($filters['group_id'] > 0) {
            $conditions .= " group_id = :group_id: AND";
            $parameters['group_id'] = $filters['group_id'];
        }

        if ($filters['is_lock'] > -1) {
            $conditions .= " is_lock = :is_lock: AND";
            $parameters['is_lock'] = $filters['is_lock'];
        }


        $conditions = rtrim($conditions, 'AND');
        //echo $conditions;exit;

        //Perform the query
        return Member::find(array(
            $conditions,
            "bind" => $parameters
        ));

    }
}
